package com.isoft.c2team3service2.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.isoft.c2team3service2.entity.UserFollow;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface UserFollowMapper extends BaseMapper<UserFollow> {
    // 检查是否已关注
    default boolean isFollowed(Integer followerId, Integer followedId) {
        LambdaQueryWrapper<UserFollow> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserFollow::getFollowerId, followerId)
                .eq(UserFollow::getFollowedId, followedId);
        return selectCount(wrapper) > 0;
    }

    // 查询用户的关注列表
    @Select("SELECT followed_user_id FROM user_follow_user WHERE user_id=#{userId}")
    List<Integer> selectFollowedIds(Integer userId);
    @Select("SELECT COUNT(1) FROM user_follow_user WHERE user_id = #{userId} AND followed_user_id = #{followedUserId}")
    int checkFollowStatus(@Param("userId") Integer userId, @Param("followedUserId") Integer followedUserId);

    @Insert("INSERT INTO user_follow_user (user_id, followed_user_id, create_time) VALUES (#{userId}, #{followedUserId}, #{createTime})")
    void insertFollowUser(@Param("userId") Integer userId, @Param("followedUserId") Integer followedUserId, @Param("createTime") LocalDateTime createTime);

    @Delete("DELETE FROM user_follow_user WHERE user_id = #{userId} AND followed_user_id = #{followedUserId}")
    int deleteFollowUser(@Param("userId") Integer userId, @Param("followedUserId") Integer followedUserId);

    @Select("SELECT followed_user_id FROM user_follow_user WHERE user_id = #{userId}")
    List<Integer> selectFollowedUserIds(@Param("userId") Integer userId);

}